﻿

namespace Huirui.Cavan.Application.Seedwork
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using Huirui.Cavan.Application.MainBoundedContext.Setting;
    using Huirui.Cavan.Core.Helper;
    using Huirui.Cavan.Core.Extension;
    using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
    using Newtonsoft.Json;

    public static class CavanWebUtility
    {
        public static string GetUserIP()
        {
            string userIP;
            if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] == null)
            {
                userIP = System.Web.HttpContext.Current.Request.UserHostAddress;
            }
            else
            {
                userIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            }
            return userIP;
        }

        public static Area GetUserSelectedCity()
        {
            string json = string.Empty;
            if (!CookieHelper.IsExsit(HuiruiProject.Settings.CityCookieName))
            {
                CookieHelper.CreateCookie(HuiruiProject.Settings.CityCookieName, DateTime.Now.AddDays(7));
                json = JsonConvert.SerializeObject(HuiruiProject.Settings.DefaultArea);
                CookieHelper.SetCookie(HuiruiAppConst.CITY, json, HuiruiProject.Settings.CityCookieName);
            }

            if (CookieHelper.IsExsit(HuiruiProject.Settings.CityCookieName))
            {
                json = CookieHelper.GetCookie(HuiruiAppConst.CITY, HuiruiProject.Settings.CityCookieName);
                if (json.IsEmpty())
                {
                    json = JsonConvert.SerializeObject(HuiruiProject.Settings.DefaultArea);
                    CookieHelper.SetCookie(HuiruiAppConst.CITY, json, HuiruiProject.Settings.CityCookieName);
                }
            }
            return JsonConvert.DeserializeObject<Area>(json);
        }
    }
}
